package xml;

import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

// ----------------------------------------------------------
//
//  Name: GameXMLreader
//  Desc: reads Game-XML and stores Game/Player-Stats
//  
//
// ----------------------------------------------------------

public class GameXMLreader 
{
	private Document xmlDoc;
	private Team team1;
	private Team team2;
	
	// Konstruktor: creates new XML-Document from String
	public GameXMLreader(String code)
	{	
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db;
			
		try 
		{		
			db = dbf.newDocumentBuilder();
			this.xmlDoc = db.parse(new InputSource(new StringReader(code)));
			read();
		
		} catch (ParserConfigurationException e1) {
			e1.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();	
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	// Method "read":
	// - reads team-Ids and team-Names and adds Players
	// - reads passed fields, ballcontacts, cards and goals for each player
	// - reads halftime and final goals for each team
	private void read()
	{
		NodeList teams = xmlDoc.getElementsByTagName("team");
		NodeList players = xmlDoc.getElementsByTagName("player");
		NodeList game = xmlDoc.getElementsByTagName("action");
		
		int id1 = Integer.parseInt(teams.item(0).getAttributes().getNamedItem("id").getNodeValue());
		int id2 = Integer.parseInt(teams.item(1).getAttributes().getNamedItem("id").getNodeValue());
		String team1name=teams.item(0).getAttributes().getNamedItem("name").getNodeValue();
		String team2name=teams.item(1).getAttributes().getNamedItem("name").getNodeValue();
		
		// create teams and add players
		team1 = new Team(id1);
		team1.setName(team1name);
		for (int i=0; i<5; i++){
			int id = Integer.parseInt(players.item(i).getAttributes().getNamedItem("id").getNodeValue());
			team1.addPlayer(id);
		}
		team2 = new Team(id2);
		team2.setName(team2name);
		for (int i=5; i<10; i++){
			int id = Integer.parseInt(players.item(i).getAttributes().getNamedItem("id").getNodeValue());
			team2.addPlayer(id);
		}
		
		// calculate moves for each player
		for (int i=0; i<game.getLength(); i++)
		{
			int pid = Integer.parseInt(game.item(i).getAttributes().getNamedItem("id").getNodeValue());
			Player p = team1.getPlayer(pid);
			if (p == null) p = team2.getPlayer(pid);
					
					String a = game.item(i).getAttributes().getNamedItem("action").getNodeValue();
					if (a.equals("run")){
						p.addFieldPassed();
						p.addFieldPassed();
					}
					if (a.equals("move")){
						p.addFieldPassed();
					}
		}
		
		// read halftime-stats (goals,cards)
		for (int i=440; i<450; i++)
		{
			int pid = Integer.parseInt(game.item(i).getAttributes().getNamedItem("id").getNodeValue());
			int cards = Integer.parseInt(game.item(i).getAttributes().getNamedItem("cards").getNodeValue());
			int goals = Integer.parseInt(game.item(i).getAttributes().getNamedItem("goals").getNodeValue());
			
			Player p = team1.getPlayer(pid);
			if (p == null) {
				team2.addHalftimeGoals(goals);
				p = team2.getPlayer(pid);
			}
			else{
				team1.addHalftimeGoals(goals);
			}
			
			p.setYellowCards(cards);
			if(cards==3){
				p.setRedCards(1);
			}
		}
		
		// read final-stats (goals,cards,ballcontacts)
		for (int i=890; i<900; i++)
		{
			System.out.println(game.getLength());
			int pid = Integer.parseInt(game.item(i).getAttributes().getNamedItem("id").getNodeValue());
			int goals = Integer.parseInt(game.item(i).getAttributes().getNamedItem("goals").getNodeValue());
			int cards = Integer.parseInt(game.item(i).getAttributes().getNamedItem("cards").getNodeValue());
			int touches = Integer.parseInt(game.item(i).getAttributes().getNamedItem("touches").getNodeValue());
			
			Player p = team1.getPlayer(pid);
			if (p == null) {
				team2.addGoals(goals);
				p = team2.getPlayer(pid);
			}
			else{
				team1.addGoals(goals);
			}
			
			p.setGoals(goals);
			p.setYellowCards(cards);
			p.setBallContacts(touches);
			if(cards==3){
				p.setRedCards(1);
			}
		}
	}

	
	// Getter-Methods
	public Team getTeam1() {
		return team1;
	}

	public Team getTeam2() {
		return team2;
	}

	public Document getXmlDoc() {
		return xmlDoc;
	}
	
}
